package org.gatorojo.listener;

import java.util.List;
import javax.servlet.*;
import org.gatorojo.model.Product;
import org.gatorojo.dao.ProductService;

public final class ContextListener
    implements ServletContextListener {
    private ServletContext context = null;

    public void contextInitialized(ServletContextEvent event) {
         context = event.getServletContext();
         
         try {
        	 ProductService service = new ProductService();
        	 List<Product> products = service.listProduct(); 
        	 context.setAttribute("products", products);
         } catch (Exception ex) {
        	 context.log("Couldn't create products list: " + ex.getMessage());
         }        
    }
      
    public void contextDestroyed(ServletContextEvent event) {
         context = event.getServletContext();
         
         if (context != null) {
           context.removeAttribute("products");           
         }
    }
}

